package com.wwwzh.himalayafm.fragment.disfragment.recommend;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wwwzh.himalayafm.BR;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.recommend.RecommendAdapter;
import com.wwwzh.himalayafm.callback.OnRecommDestroyListener;
import com.wwwzh.himalayafm.entity.recommend.RecommOneService;
import com.wwwzh.himalayafm.entity.recommend.RecommService;
import com.wwwzh.himalayafm.entity.recommend.RecommendEntity;
import com.wwwzh.himalayafm.entity.recommend.RecommendEntityOne;
import com.wwwzh.himalayafm.entity.recommend.RecommendGuessItem;
import com.wwwzh.himalayafm.entity.recommend.RecommendItem;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 *
 * 推荐界面碎片
 */
public class RecommendFragment extends Fragment implements Callback<RecommendEntity> {


    private OnRecommDestroyListener listener;
    private RecyclerView recyclerView;
    private RecommendAdapter adapter;
    private List<RecommendItem> list;
    private List<RecommendGuessItem> gList;
    private RecommendEntityOne entity;
    public RecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        Log.d("1620","create view");
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recommend_recycler);
        list = new ArrayList<>();
        gList = new ArrayList<>();
        RecommService service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecommService.class);
        service.getEntity("hotAndGuess?device=android").enqueue(this);
    }


    @Override
    public void onResponse(Call<RecommendEntity> call, Response<RecommendEntity> response) {
        list.addAll(response.body().getHotRecommends().getList());
        gList.addAll(response.body().getGuess().getList());
        //第二次下载
        RecommOneService service1 = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecommOneService.class);
        service1.getEntityOne().enqueue(new Callback<RecommendEntityOne>() {
            @Override
            public void onResponse(Call<RecommendEntityOne> call, Response<RecommendEntityOne> response) {
                entity = response.body();
                adapter = new RecommendAdapter(recyclerView.getContext(),list,gList,entity, BR.content);

                recyclerView.setAdapter(adapter);
                listener = adapter;
            }
            @Override
            public void onFailure(Call<RecommendEntityOne> call, Throwable t) {

            }
        });


    }

    @Override
    public void onFailure(Call<RecommendEntity> call, Throwable t) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (listener!=null){
            listener.onRecommDestory();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (listener!=null){
            listener.onRecommDestory();
        }
    }
}
